{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "from sklearn.preprocessing import normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {},
   "outputs": [],
   "source": [
    "xy = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\train100000.csv', delimiter=',', dtype=np.float32)\n",
    "xy3 = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\1111.csv', delimiter=',', dtype=np.float32)\n",
    "x_data=xy[:,0:-1]\n",
    "x_data = normalize(x_data, axis=0, norm='max')\n",
    "xy[:,0:-1]=x_data\n",
    "y_data=xy[:,[-1]]\n",
    "m_data=xy[:499,:]\n",
    "r_data=xy[:10000,:]\n",
    "g_data=xy[:0,:]\n",
    "F_data=xy[:,:]\n",
    "F2_data=xy3[:,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Model Inputs\n",
    "def model_inputs(real_dim, noise_dim):\n",
    "    inputs_real_ = tf.placeholder(tf.float32, shape=[None, real_dim], name='inputs_real')\n",
    "    inputs_z_ = tf.placeholder(tf.float32, shape=[None, noise_dim], name='inputs_z')\n",
    "    \n",
    "    return inputs_real_, inputs_z_\n",
    "\n",
    "def leaky_relu(x, alpha):\n",
    "    return tf.maximum(alpha * x, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generator Network\n",
    "def model_generator(z_input, out_dim, n_units=128, reuse=False, alpha=0.01):\n",
    "    # used to reuse variables, name scope\n",
    "    with tf.variable_scope('generator', reuse=reuse):\n",
    "        hidden_layer = tf.layers.dense(z_input, n_units, activation=None)\n",
    "        hidden_layer = leaky_relu(hidden_layer, alpha)\n",
    "        \n",
    "        logits = tf.layers.dense(hidden_layer, out_dim, activation=None)\n",
    "        outputs = tf.nn.sigmoid(logits)\n",
    "        \n",
    "        return outputs, logits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Discriminator Network\n",
    "def model_discriminator(input, n_units=128, reuse=False, alpha=0.1):\n",
    "    with tf.variable_scope('discriminator', reuse=reuse):\n",
    "        hidden_layer = tf.layers.dense(input, n_units, activation=tf.nn.relu)\n",
    "        #hidden_layer = leaky_relu(hidden_layer, alpha)\n",
    "        \n",
    "        logits = tf.layers.dense(hidden_layer, 1, activation=None)\n",
    "        outputs = tf.nn.sigmoid(logits)\n",
    "        \n",
    "        return outputs, logits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_size = 42\n",
    "z_dim = 21\n",
    "g_hidden_size = 128\n",
    "d_hidden_size = 128\n",
    "alpha = 0.1\n",
    "smooth = 0.1\n",
    "learning_rate = 0.001"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()  # If we don't have this, as we call this block over and over, the graph gets bigger and bigger\n",
    "\n",
    "graph = tf.Graph()\n",
    "with graph.as_default():\n",
    "    inputs_real, inputs_z = model_inputs(input_size, z_dim)\n",
    "    \n",
    "    g_outputs, g_logits = model_generator(inputs_z, input_size, n_units=g_hidden_size, reuse=False, alpha=alpha)\n",
    "    \n",
    "    d_outputs_real, d_logits_real = model_discriminator(inputs_real, n_units=d_hidden_size, reuse=False, alpha=alpha)\n",
    "    d_outputs_fake, d_logits_fake = model_discriminator(g_outputs, n_units=d_hidden_size, reuse=True, alpha=alpha)\n",
    "    \n",
    "    d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_real, labels=tf.ones_like(d_logits_real) * (1-smooth)))\n",
    "    d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.zeros_like(d_logits_fake)))\n",
    "    \n",
    "    d_loss = d_loss_real + d_loss_fake\n",
    "    g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.ones_like(d_logits_fake)))\n",
    "    \n",
    "    t_vars = tf.trainable_variables()\n",
    "    g_vars = [variable for variable in t_vars if 'generator' in variable.name]\n",
    "    d_vars = [variable for variable in t_vars if 'discriminator' in variable.name]\n",
    "    \n",
    "    # Affected Variables with var_list\n",
    "    d_optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(d_loss, var_list=d_vars)\n",
    "    g_optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(g_loss, var_list=g_vars)\n",
    "    \n",
    "    # Saving variables with var_list\n",
    "    saver = tf.train.Saver(var_list=g_vars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('C:\\\\Users\\\\SANHA\\\\Desktop\\\\train10_n.csv', 'w')\n",
    "for step in range(100000):\n",
    "    f.write(\"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f \\n\" %(xy[step,0],xy[step,1],xy[step,2],xy[step,3],xy[step,4],xy[step,5],xy[step,6],xy[step,7],xy[step,8],xy[step,9],xy[step,10],xy[step,11],xy[step,12],xy[step,13],xy[step,14],xy[step,15],xy[step,16],xy[step,17],xy[step,18],xy[step,19],xy[step,20],xy[step,21],xy[step,22],xy[step,23],xy[step,24],xy[step,25],xy[step,26],xy[step,27],xy[step,28],xy[step,29],xy[step,30],xy[step,31],xy[step,32],xy[step,33],xy[step,34],xy[step,35],xy[step,36],xy[step,37],xy[step,38],xy[step,39],xy[step,40],xy[step,41]))\n",
    "        \n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [],
   "source": [
    "xy2 = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\test20000.csv', delimiter=',', dtype=np.float32)\n",
    "tx_data=xy2[:,0:-1]\n",
    "tx_data = normalize(tx_data, axis=0, norm='max')\n",
    "xy2[:,0:-1]=tx_data\n",
    "ty_data=xy2[:,[-1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20470 / 100000: 1.486603, 0.867135"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-144-4f28da3d8b54>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     22\u001b[0m         \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m         \u001b[0msample_z\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0muniform\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mz_dim\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m# 16 Samples each epoch\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 24\u001b[1;33m         \u001b[0mgen_samples\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msess\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodel_generator\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minputs_z\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput_size\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreuse\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[0minputs_z\u001b[0m \u001b[1;33m:\u001b[0m \u001b[0msample_z\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     25\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m         \u001b[0mtemp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mgen_samples\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m41\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\py35\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m    898\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    899\u001b[0m       result = self._run(None, fetches, feed_dict, options_ptr,\n\u001b[1;32m--> 900\u001b[1;33m                          run_metadata_ptr)\n\u001b[0m\u001b[0;32m    901\u001b[0m       \u001b[1;32mif\u001b[0m \u001b[0mrun_metadata\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    902\u001b[0m         \u001b[0mproto_data\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtf_session\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTF_GetBuffer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun_metadata_ptr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\py35\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run\u001b[1;34m(self, handle, fetches, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m   1133\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfinal_fetches\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mfinal_targets\u001b[0m \u001b[1;32mor\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mfeed_dict_tensor\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1134\u001b[0m       results = self._do_run(handle, final_targets, final_fetches,\n\u001b[1;32m-> 1135\u001b[1;33m                              feed_dict_tensor, options, run_metadata)\n\u001b[0m\u001b[0;32m   1136\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1137\u001b[0m       \u001b[0mresults\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\py35\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_run\u001b[1;34m(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)\u001b[0m\n\u001b[0;32m   1314\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1315\u001b[0m       return self._do_call(_run_fn, feeds, fetches, targets, options,\n\u001b[1;32m-> 1316\u001b[1;33m                            run_metadata)\n\u001b[0m\u001b[0;32m   1317\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1318\u001b[0m       \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_prun_fn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeeds\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetches\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\py35\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_do_call\u001b[1;34m(self, fn, *args)\u001b[0m\n\u001b[0;32m   1320\u001b[0m   \u001b[1;32mdef\u001b[0m \u001b[0m_do_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1321\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1322\u001b[1;33m       \u001b[1;32mreturn\u001b[0m \u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1323\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mOpError\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1324\u001b[0m       \u001b[0mmessage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcompat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mas_text\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0me\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\py35\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_run_fn\u001b[1;34m(feed_dict, fetch_list, target_list, options, run_metadata)\u001b[0m\n\u001b[0;32m   1305\u001b[0m       \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_extend_graph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1306\u001b[0m       return self._call_tf_sessionrun(\n\u001b[1;32m-> 1307\u001b[1;33m           options, feed_dict, fetch_list, target_list, run_metadata)\n\u001b[0m\u001b[0;32m   1308\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1309\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_prun_fn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mhandle\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\envs\\py35\\lib\\site-packages\\tensorflow\\python\\client\\session.py\u001b[0m in \u001b[0;36m_call_tf_sessionrun\u001b[1;34m(self, options, feed_dict, fetch_list, target_list, run_metadata)\u001b[0m\n\u001b[0;32m   1407\u001b[0m       return tf_session.TF_SessionRun_wrapper(\n\u001b[0;32m   1408\u001b[0m           \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moptions\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfeed_dict\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfetch_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget_list\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1409\u001b[1;33m           run_metadata)\n\u001b[0m\u001b[0;32m   1410\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1411\u001b[0m       \u001b[1;32mwith\u001b[0m \u001b[0merrors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mraise_exception_on_not_ok_status\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mstatus\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "samples=[]\n",
    "normal=0\n",
    "abnormal=0\n",
    "count=0\n",
    "#line_sample=[1][42]\n",
    "with tf.Session(graph=graph) as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    f = open('C:\\\\Users\\\\SANHA\\\\Desktop\\\\gen_sample.txt', 'w')\n",
    "    for step in range(100000):\n",
    "        if count==308:\n",
    "            count=0\n",
    "        batch_images = F2_data[count].reshape([1, 42])\n",
    "        count+=1\n",
    "        batch_z = np.random.uniform(-1, 1, size=[1, z_dim])\n",
    "        \n",
    "        _ = sess.run(d_optimizer, feed_dict={inputs_real : batch_images, inputs_z : batch_z})\n",
    "        _ = sess.run(g_optimizer, feed_dict={inputs_z : batch_z})\n",
    "        loss_d, loss_g = sess.run([d_loss, g_loss], feed_dict={inputs_real : batch_images, inputs_z : batch_z})\n",
    "        #if step%1000==0:\n",
    "        #    print('step {} / {} Complete. D_Loss : {:0.3f}, G_Loss : {:0.3f}'.format(step+1, 100000, loss_d, loss_g))\n",
    "        sys.stdout.write(\"\\r%d / %d: %f, %f\" % (step, 100000, loss_d, loss_g))\n",
    "        sys.stdout.flush()\n",
    "        sample_z = np.random.uniform(-1, 1, size=[1, z_dim])  # 16 Samples each epoch\n",
    "        gen_samples, _ = sess.run(model_generator(inputs_z, input_size, reuse=True), feed_dict={inputs_z : sample_z})\n",
    "        \n",
    "        temp=gen_samples[0,41]\n",
    "        #print(temp)\n",
    "        \n",
    "        #print(temp,gen_samples[0,41])\n",
    "        f.write(\"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f \\n\" %(gen_samples[0,0],gen_samples[0,1],gen_samples[0,2],gen_samples[0,3],gen_samples[0,4],gen_samples[0,5],gen_samples[0,6],gen_samples[0,7],gen_samples[0,8],gen_samples[0,9],gen_samples[0,10],gen_samples[0,11],gen_samples[0,12],gen_samples[0,13],gen_samples[0,14],gen_samples[0,15],gen_samples[0,16],gen_samples[0,17],gen_samples[0,18],gen_samples[0,19],gen_samples[0,20],gen_samples[0,21],gen_samples[0,22],gen_samples[0,23],gen_samples[0,24],gen_samples[0,25],gen_samples[0,26],gen_samples[0,27],gen_samples[0,28],gen_samples[0,29],gen_samples[0,30],gen_samples[0,31],gen_samples[0,32],gen_samples[0,33],gen_samples[0,34],gen_samples[0,35],gen_samples[0,36],gen_samples[0,37],gen_samples[0,38],gen_samples[0,39],gen_samples[0,40],gen_samples[0,41]))\n",
    "       \n",
    "    \n",
    "        \n",
    "    print('Generating Complete. normal={}, abnormal={}'.format(normal,abnormal))\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finished\n"
     ]
    }
   ],
   "source": [
    "xy2 = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\train_11.csv', delimiter=',', dtype=np.float32)\n",
    "#xy3 = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\test.csv', delimiter=',', dtype=np.float32)\n",
    "#xy4 = np.loadtxt('C:\\\\Users\\\\SANHA\\\\Desktop\\\\test1.csv', delimiter=',', dtype=np.float32)\n",
    "\n",
    "\n",
    "\n",
    "#false data\n",
    "\n",
    "Fx_data=xy2[:,0:-1]\n",
    "Fy_data=xy2[:,[-1]]\n",
    "print(\"finished\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step:     0\tLoss: 6.614\tAcc: 0.00%\n",
      "step:    10\tLoss: 1.377\tAcc: 74.94%\n",
      "step:    20\tLoss: 0.817\tAcc: 84.77%\n",
      "step:    30\tLoss: 0.539\tAcc: 89.57%\n",
      "step:    40\tLoss: 0.411\tAcc: 89.66%\n",
      "step:    50\tLoss: 0.338\tAcc: 89.98%\n",
      "step:    60\tLoss: 0.279\tAcc: 93.21%\n",
      "step:    70\tLoss: 0.236\tAcc: 94.00%\n",
      "step:    80\tLoss: 0.208\tAcc: 94.65%\n",
      "step:    90\tLoss: 0.188\tAcc: 95.07%\n",
      "step:   100\tLoss: 0.173\tAcc: 95.24%\n",
      "step:   110\tLoss: 0.160\tAcc: 95.44%\n",
      "step:   120\tLoss: 0.150\tAcc: 95.62%\n",
      "step:   130\tLoss: 0.141\tAcc: 95.79%\n",
      "step:   140\tLoss: 0.132\tAcc: 95.97%\n",
      "step:   150\tLoss: 0.125\tAcc: 96.14%\n",
      "step:   160\tLoss: 0.118\tAcc: 96.29%\n",
      "step:   170\tLoss: 0.112\tAcc: 96.41%\n",
      "step:   180\tLoss: 0.107\tAcc: 96.49%\n",
      "step:   190\tLoss: 0.102\tAcc: 96.57%\n",
      "step:   200\tLoss: 0.098\tAcc: 96.65%\n",
      "step:   210\tLoss: 0.094\tAcc: 96.84%\n",
      "step:   220\tLoss: 0.090\tAcc: 97.01%\n",
      "step:   230\tLoss: 0.085\tAcc: 97.21%\n",
      "step:   240\tLoss: 0.081\tAcc: 97.47%\n",
      "step:   250\tLoss: 0.078\tAcc: 97.59%\n",
      "step:   260\tLoss: 0.075\tAcc: 97.72%\n",
      "step:   270\tLoss: 0.072\tAcc: 97.79%\n",
      "step:   280\tLoss: 0.070\tAcc: 97.86%\n",
      "step:   290\tLoss: 0.068\tAcc: 97.94%\n",
      "step:   300\tLoss: 0.066\tAcc: 98.04%\n",
      "step:   310\tLoss: 0.064\tAcc: 98.11%\n",
      "step:   320\tLoss: 0.062\tAcc: 98.19%\n",
      "step:   330\tLoss: 0.061\tAcc: 98.25%\n",
      "step:   340\tLoss: 0.059\tAcc: 98.32%\n",
      "step:   350\tLoss: 0.058\tAcc: 98.38%\n",
      "step:   360\tLoss: 0.057\tAcc: 98.42%\n",
      "step:   370\tLoss: 0.055\tAcc: 98.48%\n",
      "step:   380\tLoss: 0.054\tAcc: 98.51%\n",
      "step:   390\tLoss: 0.053\tAcc: 98.58%\n",
      "step:   400\tLoss: 0.051\tAcc: 98.63%\n",
      "step:   410\tLoss: 0.050\tAcc: 98.69%\n",
      "step:   420\tLoss: 0.049\tAcc: 98.74%\n",
      "step:   430\tLoss: 0.047\tAcc: 98.79%\n",
      "step:   440\tLoss: 0.046\tAcc: 98.81%\n",
      "step:   450\tLoss: 0.045\tAcc: 98.85%\n",
      "step:   460\tLoss: 0.044\tAcc: 98.85%\n",
      "step:   470\tLoss: 0.043\tAcc: 98.88%\n",
      "step:   480\tLoss: 0.042\tAcc: 98.90%\n",
      "step:   490\tLoss: 0.042\tAcc: 98.93%\n",
      "step:   500\tLoss: 0.041\tAcc: 98.93%\n",
      "step:   510\tLoss: 0.040\tAcc: 98.95%\n",
      "step:   520\tLoss: 0.039\tAcc: 98.97%\n",
      "step:   530\tLoss: 0.039\tAcc: 98.99%\n",
      "step:   540\tLoss: 0.038\tAcc: 98.99%\n",
      "step:   550\tLoss: 0.037\tAcc: 99.00%\n",
      "step:   560\tLoss: 0.036\tAcc: 99.01%\n",
      "step:   570\tLoss: 0.036\tAcc: 99.02%\n",
      "step:   580\tLoss: 0.035\tAcc: 99.03%\n",
      "step:   590\tLoss: 0.035\tAcc: 99.05%\n",
      "step:   600\tLoss: 0.034\tAcc: 99.07%\n",
      "step:   610\tLoss: 0.033\tAcc: 99.09%\n",
      "step:   620\tLoss: 0.033\tAcc: 99.10%\n",
      "step:   630\tLoss: 0.032\tAcc: 99.13%\n",
      "step:   640\tLoss: 0.032\tAcc: 99.14%\n",
      "step:   650\tLoss: 0.031\tAcc: 99.15%\n",
      "step:   660\tLoss: 0.031\tAcc: 99.17%\n",
      "step:   670\tLoss: 0.031\tAcc: 99.19%\n",
      "step:   680\tLoss: 0.030\tAcc: 99.21%\n",
      "step:   690\tLoss: 0.030\tAcc: 99.23%\n",
      "step:   700\tLoss: 0.029\tAcc: 99.24%\n",
      "step:   710\tLoss: 0.029\tAcc: 99.25%\n",
      "step:   720\tLoss: 0.029\tAcc: 99.26%\n",
      "step:   730\tLoss: 0.028\tAcc: 99.28%\n",
      "step:   740\tLoss: 0.028\tAcc: 99.28%\n",
      "step:   750\tLoss: 0.028\tAcc: 99.31%\n",
      "step:   760\tLoss: 0.027\tAcc: 99.31%\n",
      "step:   770\tLoss: 0.027\tAcc: 99.32%\n",
      "step:   780\tLoss: 0.027\tAcc: 99.33%\n",
      "step:   790\tLoss: 0.026\tAcc: 99.34%\n",
      "step:   800\tLoss: 0.026\tAcc: 99.35%\n",
      "step:   810\tLoss: 0.026\tAcc: 99.35%\n",
      "step:   820\tLoss: 0.026\tAcc: 99.35%\n",
      "step:   830\tLoss: 0.025\tAcc: 99.36%\n",
      "step:   840\tLoss: 0.025\tAcc: 99.37%\n",
      "step:   850\tLoss: 0.025\tAcc: 99.37%\n",
      "step:   860\tLoss: 0.025\tAcc: 99.38%\n",
      "step:   870\tLoss: 0.024\tAcc: 99.39%\n",
      "step:   880\tLoss: 0.024\tAcc: 99.39%\n",
      "step:   890\tLoss: 0.024\tAcc: 99.40%\n",
      "step:   900\tLoss: 0.024\tAcc: 99.40%\n",
      "step:   910\tLoss: 0.023\tAcc: 99.40%\n",
      "step:   920\tLoss: 0.023\tAcc: 99.41%\n",
      "step:   930\tLoss: 0.023\tAcc: 99.41%\n",
      "step:   940\tLoss: 0.023\tAcc: 99.42%\n",
      "step:   950\tLoss: 0.023\tAcc: 99.42%\n",
      "step:   960\tLoss: 0.022\tAcc: 99.42%\n",
      "step:   970\tLoss: 0.022\tAcc: 99.43%\n",
      "step:   980\tLoss: 0.022\tAcc: 99.43%\n",
      "step:   990\tLoss: 0.022\tAcc: 99.44%\n",
      "step:  1000\tLoss: 0.022\tAcc: 99.43%\n",
      "true=19765 false: 235 acc: 0.9882\n",
      "normal=158,apache2=0,back=4,buffer_overflow=3,ftp_write=0,guess_passwd=1,httptunnel=0,imap=0,ipsweep=14,land=1,loadmodule=4,mailboml=0,mscan=0,multihop=1,named=0,neptune=1,nmap=9,perl=1,phf=0,pod=0,portsweep=8,processtable=0,ps=0,rootkit=0,saint=0,satan=18,sendmail=0,smurf=1,snmpgetattack=0,snmpguess=0,sqlattack=0,teardrop=0,updstorm=0,warezmaster=3,warezclient=8,worm=0,xlock=0,    xsnoop=0,xterm=0,spy=0\n"
     ]
    }
   ],
   "source": [
    "#초기화\n",
    "normal= 0\n",
    "apache2=0\n",
    "back =0\n",
    "buffer_overflow =0\n",
    "ftp_write =0\n",
    "guess_passwd =0\n",
    "httptunnel =0\n",
    "imap =0\n",
    "ipsweep =0\n",
    "land =0\n",
    "loadmodule =0\n",
    "mailbomb =0\n",
    "mscan =0\n",
    "multihop =0\n",
    "named =0\n",
    "neptune =0\n",
    "nmap =0\n",
    "perl =0\n",
    "phf =0\n",
    "pod =0\n",
    "portsweep =0\n",
    "processtable =0\n",
    "ps =0\n",
    "rootkit =0\n",
    "saint =0\n",
    "satan =0\n",
    "sendmail =0\n",
    "smurf =0\n",
    "snmpgetattack =0\n",
    "snmpguess =0\n",
    "sqlattack =0\n",
    "teardrop =0\n",
    "udpstorm =0\n",
    "warezmaster =0\n",
    "warezclient =0\n",
    "worm =0\n",
    "xlock =0\n",
    "xsnoop =0\n",
    "xterm =0\n",
    "spy =0\n",
    "\n",
    "#분류\n",
    "nb_classes=40\n",
    "\n",
    "X=tf.placeholder(tf.float32,[None,41])\n",
    "Y=tf.placeholder(tf.int32,[None,1])\n",
    "\n",
    "Y_one_hot=tf.one_hot(Y,nb_classes)\n",
    "Y_one_hot=tf.reshape(Y_one_hot,[-1,nb_classes])\n",
    "\n",
    "W1=tf.Variable(tf.random_normal([41,41]),name='weight1')\n",
    "b1=tf.Variable(tf.random_normal([41]),name='bias1')\n",
    "layer1=tf.sigmoid(tf.matmul(X,W1)+b1)\n",
    "\n",
    "W2=tf.Variable(tf.random_normal([41,41]),name='weight2')\n",
    "b2=tf.Variable(tf.random_normal([41]),name='bias2')\n",
    "layer2=tf.sigmoid(tf.matmul(layer1,W2)+b2)\n",
    "\n",
    "W3=tf.Variable(tf.random_normal([41,nb_classes]),name='weight3')\n",
    "b3=tf.Variable(tf.random_normal([nb_classes]),name='bias3')\n",
    "logits=tf.matmul(layer2,W3)+b3\n",
    "hypothesis=tf.nn.softmax(logits)\n",
    "\n",
    "cost_i=tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=Y_one_hot)\n",
    "\n",
    "cost=tf.reduce_mean(cost_i)\n",
    "optimizer=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)\n",
    "\n",
    "prediction=tf.argmax(hypothesis,1) #가능성을 퍼센트로~~\n",
    "correct_prediction=tf.equal(prediction,tf.arg_max(Y_one_hot,1))\n",
    "accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    f = open('C:\\\\Users\\\\SANHA\\\\Desktop\\\\false_data.txt', 'a+')\n",
    "    for step in range(1001):\n",
    "        sess.run(optimizer,feed_dict={X:Fx_data,Y:Fy_data})\n",
    "        #print(\"training by gan sample\")\n",
    "        if step %10==0:\n",
    "            loss,acc=sess.run([cost,accuracy],feed_dict={X:Fx_data,Y:Fy_data})\n",
    "            print(\"step: {:5}\\tLoss: {:.3f}\\tAcc: {:.2%}\".format(step,loss,acc))\n",
    "  \n",
    "    \n",
    "#m_data=np.append(m_data,gen_samples,axis=0)\n",
    "\n",
    "    tr=0\n",
    "    fa=0\n",
    "    total=0\n",
    "    pred = sess.run(prediction, feed_dict={X: tx_data})\n",
    "    for p, y in zip(pred, ty_data.flatten()):\n",
    "            if(p==int(y)):\n",
    "                tr=tr+1\n",
    "                total=total+1\n",
    "            else:\n",
    "                fa=fa+1\n",
    "                if(int(y)==0):\n",
    "                    normal+=1\n",
    "                    \n",
    "                elif(int(y)==1):\n",
    "                    apache2+=1\n",
    "                elif(int(y)==2):\n",
    "                    back+=1\n",
    "\n",
    "                elif(int(y)==3):\n",
    "                    buffer_overflow+=1\n",
    "                elif(int(y)==4):\n",
    "                    ftp_write+=1\n",
    "                elif(int(y)==5):\n",
    "                    guess_passwd+=1\n",
    "                elif(int(y)==6):\n",
    "                    httptunnel+=1\n",
    "                elif(int(y)==7):\n",
    "                    imap+=1\n",
    "                elif(int(y)==8):\n",
    "                    ipsweep+=1\n",
    "                elif(int(y)==9):\n",
    "                    land+=1\n",
    "                elif(int(y)==10):\n",
    "                    loadmodule+=1\n",
    "                elif(int(y)==11):\n",
    "                    mailbomb+=1\n",
    "                elif(int(y)==12):\n",
    "                    mscan+=1\n",
    "                elif(int(y)==13):\n",
    "                    multihop+=1\n",
    "                elif(int(y)==14):\n",
    "                    named+=1\n",
    "                elif(int(y)==15):\n",
    "                    neptune+=1\n",
    "                elif(int(y)==16):\n",
    "                    nmap+=1\n",
    "                elif(int(y)==17):\n",
    "                    perl+=1\n",
    "                elif(int(y)==18):\n",
    "                    phf+=1\n",
    "                elif(int(y)==19):\n",
    "                    pod+=1\n",
    "                elif(int(y)==20):\n",
    "                    portsweep+=1\n",
    "                elif(int(y)==21):\n",
    "                    processtable+=1\n",
    "                elif(int(y)==22):\n",
    "                    ps+=1\n",
    "                elif(int(y)==23):\n",
    "                    rootkit+=1\n",
    "                elif(int(y)==24):\n",
    "                    saint+=1\n",
    "                elif(int(y)==25):\n",
    "                    satan+=1\n",
    "                elif(int(y)==26):\n",
    "                    sendmail+=1\n",
    "                elif(int(y)==27):\n",
    "                    smurf+=1\n",
    "                elif(int(y)==28):\n",
    "                    snmpgetattack+=1\n",
    "                elif(int(y)==29):\n",
    "                    snmpguess+=1\n",
    "                elif(int(y)==30):\n",
    "                    sqlattack+=1\n",
    "                elif(int(y)==31):\n",
    "                    teardrop+=1\n",
    "                elif(int(y)==32):\n",
    "                    udpstorm+=1\n",
    "                elif(int(y)==33):\n",
    "                    warezmaster+=1\n",
    "                elif(int(y)==34):\n",
    "                    warezclient+=1\n",
    "                    f.write(\"%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f \\n\" \\\n",
    "                        %(tx_data[total,0],tx_data[total,1],tx_data[total,2],tx_data[total,3],tx_data[total,4],tx_data[total,5],tx_data[total,6],tx_data[total,7],tx_data[total,8],tx_data[total,9],tx_data[total,10],tx_data[total,11],tx_data[total,12],tx_data[total,13],tx_data[total,14],tx_data[total,15],tx_data[total,16],tx_data[total,17],tx_data[total,18],tx_data[total,19],tx_data[total,20],tx_data[total,21],tx_data[total,22],tx_data[total,23],tx_data[total,24],tx_data[total,25],tx_data[total,26],tx_data[total,27],tx_data[total,28],tx_data[total,29],tx_data[total,30],tx_data[total,31],tx_data[total,32],tx_data[total,33],tx_data[total,34],tx_data[total,35],tx_data[total,36],tx_data[total,37],tx_data[total,38],tx_data[total,39],tx_data[total,40],ty_data[total,0]))\n",
    "\n",
    "                elif(int(y)==35):\n",
    "                    worm+=1\n",
    "                elif(int(y)==36):\n",
    "                    xlock+=1\n",
    "                elif(int(y)==37):\n",
    "                    xsnoop+=1\n",
    "                elif(int(y)==38):\n",
    "                    xterm+=1\n",
    "                elif(int(y)==39):\n",
    "                    spy+=1\n",
    "\n",
    "                total=total+1\n",
    "\n",
    "\n",
    "\n",
    "                #print(\"[{}] Prediction: {} Real Y: {}\".format(p == int(y), p, int(y)))\n",
    "       \n",
    "    print(\"true={} false: {} acc: {:0.4f}\".format(tr,fa,tr/(tr+fa)))\n",
    "    print(\"normal={},apache2={},back={},buffer_overflow={},ftp_write={},guess_passwd={},httptunnel={},imap={},ipsweep={},land={},loadmodule={},mailboml={},mscan={},multihop={},named={},neptune={},nmap={},perl={},phf={},pod={},portsweep={},processtable={},ps={},rootkit={},saint={},satan={},sendmail={},smurf={},snmpgetattack={},snmpguess={},sqlattack={},teardrop={},updstorm={},warezmaster={},warezclient={},worm={},xlock={},\\\n",
    "    xsnoop={},xterm={},spy={}\".format(normal,apache2,back,buffer_overflow,ftp_write,guess_passwd,httptunnel,imap,ipsweep,land,loadmodule,mailbomb,mscan,multihop,named,neptune,nmap,perl,phf,pod,portsweep,processtable,ps,rootkit,saint,satan,sendmail,smurf,snmpgetattack,snmpguess,sqlattack,teardrop\\\n",
    "                  ,udpstorm,warezmaster,warezclient,worm,xlock,xsnoop,xterm,spy))\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
